import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/entrys/rp/erp_odd_numbers_entity.dart';
import 'package:xiongan_app_yy/provider/erp_info.dart';

class NumbercCountButton extends StatefulWidget {
  //final dynamic number;

  final int maxNumber;
  final double width;
  final double buttonWidth;
  final double height;
  bool enabled;
  final String hintText;
  ValueChanged<String> onChangedFuns;
  final TextEditingController textEditingController;
  NumbercCountButton({
    this.maxNumber = 0,
    this.width = double.infinity,
    this.height = 26,
    this.buttonWidth = 26,
    @required this.textEditingController,
    this.enabled=true,
    this.hintText,
    this.onChangedFuns
  });

  @override
  State<StatefulWidget> createState() {
    return NumbercCountButtonState();
  }
}

class NumbercCountButtonState extends State<NumbercCountButton> {
  int number1;
  @override
  void initState() {
    number1=widget.textEditingController.text==""?0:int.parse(widget.textEditingController.text);
  }

  @override
  Widget build(BuildContext context) {
    widget.textEditingController.selection = TextSelection.fromPosition(
        TextPosition(
            affinity: TextAffinity.downstream,
            offset: widget.textEditingController.text.length));
    return Container(
      width: widget.width,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
          border:
              Border.all(width: 1, color: Colors.black12) //设置所有的边框宽度为1 颜色为浅灰
          ),
      child: Row(
        children: <Widget>[
          _reduceBtn(context),
          Expanded(child: _countArea(context)),
          _addBtn(context)
        ],
      ),
    );
  }

  //减少按钮
  Widget _reduceBtn(context) {
    return InkWell(
      onTap: () {
        FocusScope.of(context).requestFocus(FocusNode());
        if(widget.enabled){
          if (number1 > 0) {
            setState(() {
              number1 = number1 - 1;
              widget.textEditingController.text = number1.toString();
            });
          }
        }
        widget.onChangedFuns(number1.toString());
      },
      child: Container(
        width: widget.buttonWidth,
        height: widget.height,
        alignment: Alignment.center, //上下左右都居中
        decoration: BoxDecoration(
          color: Colors.transparent, //按钮颜色大于1是白色，小于1是灰色
        ),
        child: Text('—',style: TextStyle(fontSize: 16)),
      ),
    );
  }

  //加号
  Widget _addBtn(context) {
    return InkWell(
      onTap: () {
        FocusScope.of(context).requestFocus(FocusNode());
        if(widget.enabled) {
          if (widget.maxNumber == 0) {
            //无最大值
            setState(() {
              number1 = number1 + 1;
              widget.textEditingController.text = number1.toString();
            });
            widget.onChangedFuns(number1.toString());
          } else {
            //有最大值
            if (number1 < widget.maxNumber) {
              setState(() {
                number1 = number1 + 1;
                widget.textEditingController.text = number1.toString();
              });
            }
            widget.onChangedFuns(number1.toString());
          }
        }
      },
      child: Container(
        width: widget.buttonWidth,
        height: widget.height,
        alignment: Alignment.center, //上下左右都居中
        decoration: BoxDecoration(
          color: Colors.transparent,
        ),
        child: Text('+',style: TextStyle(fontSize: 18)),
      ),
    );
  }

  //中间数量显示区域
  Widget _countArea(context) {
    return Container(
        height: widget.height,
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
                left: BorderSide(width: 1.0, color: Colors.black12),
                right: BorderSide(width: 1.0, color: Colors.black12))),
        alignment: Alignment.center,
        child: TextField(
          textAlign: TextAlign.center,
          enabled: widget.enabled,
          decoration: InputDecoration(
              hintText: widget.hintText,
              hintStyle: TextStyle(fontSize: 18),
              border: InputBorder.none,
              contentPadding: EdgeInsets.only(top: 5, bottom: 5)),
          style: TextStyle(fontSize: 20),
          controller: widget.textEditingController,
          keyboardType: TextInputType.number,
          inputFormatters: [
            WhitelistingTextInputFormatter(RegExp("[0-9]")),
          ],
          onChanged: (a) {
            if (a.length < 1) {
              setState(() {
                number1 = 0;
              });
            } else {
              number1 = int.parse(a);
            }

            if (widget.maxNumber != 0) {
              if (number1 >= 0 && number1 <= widget.maxNumber) {

              } else {
               setState(() {
                 number1 = widget.maxNumber;
               });

              }
            }
            widget.textEditingController.text=number1.toString();
            widget.onChangedFuns(number1.toString());
          },
        ));
  }
}
